import Vue from "vue";
import Router from "vue-router";
// 路由懒加载

const Login = () => import("./views/Login.vue");
const Home = () => import("./views/Home.vue");
const Welcome = () => import("./views/Welcome.vue");
const User = () => import("./views/User/User.vue");
const Right = () => import( "./views/Auth/Right.vue");
const Roles = () => import( "./views/Auth/Roles.vue");
const Categories = () => import( "./views/Goods/Categories.vue");
const Params = () => import("./views/Goods/Params.vue");
const Goods = () => import( "./views/Goods/Goods.vue");
const Addgoods = () => import("./views/Goods/Addgoods.vue");
const Orders = () => import( "./views/orders/Orders.vue");
const Reports = () => import( "./views/reports/Reports.vue");

Vue.use(Router);
var router = new Router({
  routes: [
    {
      path: "/login",
      component: Login
    },
    {
      path: "/home",
      component: Home,
      redirect: "/welcome",
      children: [
        { path: "/welcome", component: Welcome },
        { path: "/users", component: User },
        { path: "/rights", component: Right },
        { path: "/roles", component: Roles },
        { path: "/categories", component: Categories },
        { path: "/params", component: Params },
        { path: "/goods", component: Goods },
        { path: "/addgoods", component: Addgoods },
        { path: "/orders", component: Orders },
        { path: "/reports", component: Reports }
      ]
    }
  ]
});

router.beforeEach((to, from, next) => {
  var token = window.sessionStorage.getItem("token");
  if (token === null && to.path !== "/login") {
    return next("/login");
  }
  next();
});
export default router;
